package com.cskaoyan.service;

import com.cskaoyan.bean.AdminAdminListVo;
import com.cskaoyan.bean.MarketAdmin;
import com.cskaoyan.bean.MarketAdminExample;
import com.cskaoyan.bean.MarketRole;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketAdminMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.EmptyStackException;
import java.util.List;

/**
 * @author gatsby
 * @since 2022/10/11 20:12
 */
@Service
public class AdminAdminServiceImpl implements AdminAdminService {

    @Autowired
    MarketAdminMapper marketAdminMapper;

    /**
     * 分页查询全部管理员的业务逻辑
     *
     * @param basePageInfo 分页需要的基本数据
     * @return com.cskaoyan.bean.common.CommonData<com.cskaoyan.bean.AdminAdminListVo>
     * @author gatsby
     * @since 2022/10/11 20:42
     */
    @Override
    public CommonData<AdminAdminListVo> queryAllAdminWithPageLimit(BasePageInfo basePageInfo, String username) {
        //执行之前，使用pageHelper开启分页
        PageHelper.startPage(basePageInfo.getPage(), basePageInfo.getLimit());

        //分页查询全部的管理员
        List<AdminAdminListVo> adminAdminListVoList = marketAdminMapper.selectWithPageLimit(basePageInfo, username);
        PageInfo<AdminAdminListVo> pageInfo = new PageInfo<>(adminAdminListVoList);
        //获取总页数和总个数
        int pages = pageInfo.getPages();
        int total = (int) pageInfo.getTotal();

        CommonData<AdminAdminListVo> commonData = new CommonData<>();
        commonData.setList(adminAdminListVoList);
        commonData.setLimit(basePageInfo.getLimit());
        commonData.setPage(basePageInfo.getPage());
        commonData.setTotal(total);
        commonData.setPages(pages);
        return commonData;
    }

    /**
     * 创建管理员的业务逻辑
     *
     * @param marketAdmin
     * @return com.cskaoyan.bean.common.CommonData<com.cskaoyan.bean.MarketAdmin>
     * @author gatsby
     * @since 2022/10/12 13:49
     */
    @Override
    public MarketAdmin createAdmin(MarketAdmin marketAdmin) {

        //TODO:需要对原始的密码进行加密,后面学了MD5加密解密再做，
        // 这里先简单处理

        //先判断用户名是否已存在
        MarketAdminExample example = new MarketAdminExample();
        example.setDistinct(true);
        MarketAdminExample.Criteria criteria = example.createCriteria();
        criteria.andUsernameEqualTo(marketAdmin.getUsername());
        long count = marketAdminMapper.countByExample(example);
        if (count > 0) {
            MarketAdmin marketAdmin1 = new MarketAdmin();
            marketAdmin1.setId(-1);
            return marketAdmin1;
        }

        //在判断密码是否小于6位
        if (marketAdmin.getPassword().length() < 6) {
            MarketAdmin marketAdmin1 = new MarketAdmin();
            marketAdmin1.setId(-2);
            return marketAdmin1;
        }


        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(new Date());
        try {
            Date time = sdf.parse(format);
            marketAdmin.setAddTime(time);
            marketAdmin.setUpdateTime(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        marketAdminMapper.insertSelective(marketAdmin);

        return marketAdmin;
    }

    /**
     * 修改管理员信息的业务逻辑
     *
     * @param marketAdmin
     * @return com.cskaoyan.bean.MarketAdmin
     * @author gatsby
     * @since 2022/10/12 14:53
     */
    @Transactional
    @Override
    public MarketAdmin updateAdmin(MarketAdmin marketAdmin) {

        //TODO:需要对原始的密码进行加密,后面学了MD5加密解密再做，
        // 这里先简单处理


        //先判断用户名是否已存在
        MarketAdminExample example = new MarketAdminExample();
        example.setDistinct(true);
        MarketAdminExample.Criteria criteria = example.createCriteria();
        criteria.andIdNotEqualTo(marketAdmin.getId());
        criteria.andUsernameEqualTo(marketAdmin.getUsername());
        criteria.andDeletedEqualTo(false);
        long count = marketAdminMapper.countByExample(example);
        if (count > 0) {
            MarketAdmin marketAdmin1 = new MarketAdmin();
            marketAdmin1.setId(-1);
            return marketAdmin1;
        }

        //再判断密码是否小于6位
        if (marketAdmin.getPassword().length() < 6) {
            MarketAdmin marketAdmin1 = new MarketAdmin();
            marketAdmin1.setId(-2);
            return marketAdmin1;
        }


        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(new Date());
        try {
            Date time = sdf.parse(format);
            //更新修改的时间
            marketAdmin.setUpdateTime(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        marketAdminMapper.updateByPrimaryKeySelective(marketAdmin);

        return marketAdmin;
    }

    /**
     * 删除管理员的业务逻辑
     *
     * @param marketAdmin
     * @return void
     * @author gatsby
     * @since 2022/10/12 15:09
     */
    @Transactional
    @Override
    public void deleteAdmin(MarketAdmin marketAdmin) {
        //执行逻辑删除
        marketAdmin.setDeleted(true);
        //marketAdminMapper
        marketAdminMapper.updateByPrimaryKeySelective(marketAdmin);
    }


}
